package com.hirsi.core.thread;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject;
import com.hirsi.core.util.BaiduDiskUtil;
import com.hirsi.core.util.DbUtil;

import java.io.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 分片上传线程
 */
public class SplitUploadThread implements Runnable{

    private String path;

    private Long start;

    private Long end;

    private String outPath;

    private String remotePath;

    private String bdUploadId;

    private Integer i;

    private Integer uploadId;

    public SplitUploadThread(String path, Long start, Long end, String outPath, String remotePath, String bdUploadId, Integer i, Integer uploadId) {
        this.path = path;
        this.start = start;
        this.end = end;
        this.outPath = outPath;
        this.remotePath = remotePath;
        this.bdUploadId = bdUploadId;
        this.i = i;
        this.uploadId = uploadId;
    }

    @Override
    public void run() {

        try (RandomAccessFile raf = new RandomAccessFile(path, "r")) {
            byte[] buffer = new byte[(int) (end - start)];
            raf.seek(start);
            raf.readFully(buffer);
            OutputStream os = new FileOutputStream(outPath);
            os.write(buffer);
            os.flush();
            os.close();
            File uploadFile = new File(outPath);
            JSONObject object = BaiduDiskUtil.superFile2(remotePath, bdUploadId, i, uploadFile);
            //System.out.println("分片上传返回：" + object);
            PreparedStatement ps = DbUtil.connection.prepareStatement("insert into sn_upload_split (upload_id, bd_upload_id, split_index, path, size, state, start, end, md5, remote_md5) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            ps.setInt(1, uploadId);
            ps.setString(2, bdUploadId);
            ps.setInt(3, i);
            ps.setString(4, outPath);
            ps.setLong(5, end - start);
            ps.setLong(7, start);
            ps.setLong(8, end);
            ps.setString(9, MD5.create().digestHex(uploadFile));
            if (object.containsKey("partseq") && object.getInt("partseq").equals(i)) {
                //分片上传成功删除分片
                String md5 = object.getStr("md5");
                uploadFile.delete();
                ps.setInt(6, 1);
                ps.setString(10, md5);
                /*PreparedStatement ps1 = DbUtil.connection.prepareStatement("update sn_upload set progress = ? where id = ?");
                ps1.setInt(1, remoteMd5s.size());
                ps1.setInt(2, id);
                ps1.executeUpdate();*/
            } else {
                ps.setInt(6, -1);
            }
            ps.execute();
        } catch (IOException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        /*try (RandomAccessFile raf = new RandomAccessFile("F:\\config\\tmp\\01行尸走肉第一季\\01行尸走肉第一季.part1.rar", "r")) {
            long start = 2143289344L;
            long end = 2147483648L;
            byte[] buffer = new byte[(int) (end - start)];
            raf.seek(start);
            raf.readFully(buffer);
            System.out.println(MD5.create().digestHex(buffer));
            *//*
            OutputStream os = new FileOutputStream("F:\\config\\tmp\\01行尸走肉第一季\\01行尸走肉第一季.part1.rar.temp511");
            os.write(buffer);
            os.flush();
            os.close();*//*
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            InputStream is = new FileInputStream("F:\\config\\tmp\\01行尸走肉第一季\\01行尸走肉第一季.part1.512.temp");
            byte[] bytes = new byte[4194304];
            is.read(bytes);
            System.out.println(MD5.create().digestHex(bytes));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
    }
}
